local skynet = require "skynet"
local filelog = require "filelog"
local configdao = require "configdao"
local timetool = require "timetool"
local tabletool = require "tabletool"
local helperbase = require "helperbase"
local playerdatadao = require "playerdatadao"
local gamelog = require "gamelog"

local AgentHelper = helperbase:new({})


function AgentHelper:agentexit()
	self.server:agentexit()
end

function AgentHelper:start_timer()
	local now_time = timetool.get_time() * 100
	local timeout = self.server.session_timeout*100 - (now_time - self.server.session_begin_time)
	skynet.fork(function()
		skynet.sleep(timeout)
		self.server:timeout()
	end)
end

function AgentHelper:create_session(id, conf)
	self.server.client_fd = conf.client
	self.server.watchdog = conf.watchdog
	self.server.session_id = id
	self.server.session_begin_time = timetool.get_time() * 100
	self.server.session_timeout = configdao.get_common_conf("agentsessiontimeout")
	if self.server.session_timeout == nil 
		or self.server.session_timeout == 0 then
		self.server.session_timeout = 5
	end 

	self:start_timer()
 	return true
end

function AgentHelper:save_player_coin(money, rid, number, reason, key)
	local beforetotal = money.coin
	local aftertotal = 0
	if money.coin + number >= 0 then
		money.coin = money.coin + number
		if money.coin > money.maxcoinnum then money.maxcoinnum = money.coin end
	else
		money.coin = 0
	end
	aftertotal = money.coin
	playerdatadao.save_player_money("update", rid, money)
	gamelog.write_player_coinlog(rid, reason, key, number, beforetotal, aftertotal)
end

function AgentHelper:save_player_gemstone(money, rid, number, reason, key)
	local beforetotal = 0
	local aftertotal = 0
	if money.gemstone + number >= 0 then
		money.gemstone = money.gemstone + number
		if money.gemstone > money.maxgemstone then money.maxgemstone = money.gemstone end
	else
		money.gemstone = 0
	end
	aftertotal = money.gemstone
	playerdatadao.save_player_money("update",rid, money)
	gamelog.write_player_gemstonelog(rid, reason, key, number, beforetotal, aftertotal)
end

return	AgentHelper  
